package com.alibaba.nacos.plugin.datasource.impl.kingbase8;

import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
import com.alibaba.nacos.plugin.datasource.dialect.DatabaseDialect;
import com.alibaba.nacos.plugin.datasource.manager.DatabaseDialectManager;
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;

import java.util.List;

import static com.alibaba.nacos.plugin.datasource.constants.DefaultConstant.DEFAULT_TENANT_ID;

public abstract class AbstractKingbase8Mapper extends AbstractMapper {

    private final DatabaseDialect databaseDialect;

    public AbstractKingbase8Mapper() {
        this.databaseDialect = DatabaseDialectManager.getInstance().getDialect(getDataSource());
    }

    public DatabaseDialect getDatabaseDialect() {
        return databaseDialect;
    }

    @Override
    public String getDataSource() {
        return DatabaseTypeConstant.KINGBASE8;
    }

    @Override
    public String select(List<String> columns, List<String> where) {
        StringBuilder sql = new StringBuilder();
        String method = "SELECT ";
        sql.append(method);
        for (int i = 0; i < columns.size(); i++) {
            sql.append(columns.get(i));
            if (i == columns.size() - 1) {
                sql.append(" ");
            } else {
                sql.append(",");
            }
        }
        sql.append("FROM ");
        sql.append(getTableName());
        sql.append(" ");

        if (where.size() == 0) {
            return sql.toString();
        }

        sql.append("WHERE ");
        for (int i = 0; i < where.size(); i++) {
            String condition = where.get(i);
            if (StringUtils.equalsIgnoreCase(condition, "tenant_id")) {
                sql.append("tenant_id").append(" = ").append("NVL(?, '").append(DEFAULT_TENANT_ID)
                        .append("')");
            } else {
                sql.append(condition).append(" = ").append("?");
            }

            if (i != where.size() - 1) {
                sql.append(" AND ");
            }
        }
        return sql.toString();
    }

    @Override
    public String insert(List<String> columns) {
        // 使用增强的 for 循环遍历列表并打印元素

        StringBuilder sql = new StringBuilder();
        String method = "INSERT INTO ";
        sql.append(method);
        sql.append(this.getTableName());
        int size = columns.size();
        sql.append("(");

        int i;
        for (i = 0; i < size; ++i) {
            if(((String)columns.get(i)).equals("gmt_create@NOW()"))
            {
                continue;
            }
            if(((String)columns.get(i)).equals("gmt_modified@NOW()"))
            {
                continue;
            }
            sql.append((String) columns.get(i));
            if (i != columns.size() - 1) {
                sql.append(", ");
            }
        }

        sql.append(") ");
        sql.append("VALUES");
        sql.append("(");

        for (i = 0; i < size; ++i) {
            String column = (String) columns.get(i);
            if(((String)columns.get(i)).equals("gmt_create@NOW()"))
            {
                continue;
            }
            if(((String)columns.get(i)).equals("gmt_modified@NOW()"))
            {
                continue;
            }
            if (StringUtils.equalsIgnoreCase(column, "tenant_id")) {
                sql.append("NVL(").append("?,'").append(DEFAULT_TENANT_ID).append("')");
            }
			else{
				sql.append("?");
			}
            if (i != columns.size() - 1) {
                sql.append(",");
            }
        }

        sql.append(")");
        return sql.toString();
    }

    @Override
    public String update(List<String> columns, List<String> where) {
        StringBuilder sql = new StringBuilder();
        String method = "UPDATE ";
        sql.append(method);
        sql.append(getTableName()).append(" ").append("SET ");

        for (int i = 0; i < columns.size(); i++) {
            sql.append(columns.get(i)).append(" = ").append("?");
            if (i != columns.size() - 1) {
                sql.append(",");
            }
        }

        if (where.size() == 0) {
            return sql.toString();
        }

        sql.append(" WHERE ");

        for (int i = 0; i < where.size(); i++) {
            String condition = where.get(i);
            if (StringUtils.equalsIgnoreCase(condition, "tenant_id")) {
                sql.append("tenant_id").append(" = ").append("NVL(?, '").append(DEFAULT_TENANT_ID)
                        .append("')");
            } else {
                sql.append(condition).append(" = ").append("?");
            }
            if (i != where.size() - 1) {
                sql.append(" AND ");
            }
        }
        return sql.toString();
    }

    @Override
    public String delete(List<String> params) {
        StringBuilder sql = new StringBuilder();
        String method = "DELETE ";
        sql.append(method).append("FROM ").append(getTableName()).append(" ")
                .append("WHERE ");
        for (int i = 0; i < params.size(); i++) {

            String condition = params.get(i);
            if (StringUtils.equalsIgnoreCase(condition, "tenant_id")) {
                sql.append("tenant_id").append(" = ").append("NVL(?, '").append(DEFAULT_TENANT_ID)
                        .append("')");
            } else {
                sql.append(condition).append(" = ").append("?");
            }

            if (i != params.size() - 1) {
                sql.append("AND ");
            }
        }

        return sql.toString();
    }

    @Override
    public String count(List<String> where) {
        StringBuilder sql = new StringBuilder();
        String method = "SELECT ";
        sql.append(method);
        sql.append("COUNT(*) FROM ");
        sql.append(getTableName());
        sql.append(" ");

        if (null == where || where.size() == 0) {
            return sql.toString();
        }

        sql.append("WHERE ");
        for (int i = 0; i < where.size(); i++) {
            String condition = where.get(i);
            if (StringUtils.equalsIgnoreCase(condition, "tenant_id")) {
                sql.append("tenant_id").append(" = ").append("NVL(?, '").append(DEFAULT_TENANT_ID)
                        .append("')");
            } else {
                sql.append(condition).append(" = ").append("?");
            }
            if (i != where.size() - 1) {
                sql.append(" AND ");
            }
        }
        return sql.toString();
    }


}
